Imports System.IO
Imports System.Data.SqlClient
Imports System
Imports System.Text
Imports System.Data.Odbc
Public Class FrmMove

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        If Trim(txtFromEventID.Text) = "" Then
            Exit Sub
        End If
        If Trim(cbNewEvent.Text) = "" Then
            Exit Sub
        End If
        If GV_MOVE_COPY_TYPE = "MOVE" Then
            ChangeEventID()
        End If
        If GV_MOVE_COPY_TYPE = "COPY" Then
            CopyEventID()
        End If
        Close()
        FrmDiagnosis.Close()
        FrmDiagnosis.Show()
        FrmDiagnosis.Focus()
    End Sub
    Private Sub CopyEventID()
        Dim response As Integer

        response = MsgBox("Are you sure want to duplicate this record to new event?", MsgBoxStyle.YesNo, "Copy")
        If response = 7 Then
            Exit Sub
        End If

        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("spCopyEvent", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()

        myCommand.Parameters.Add("@FromEvent_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@ToEvent_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Type", SqlDbType.VarChar)
        myCommand.Parameters.Add("@TypeID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Pt_id1", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)

        myCommand.Parameters("@FromEvent_ID").Value = GV_SELECTED_EVENTID
        myCommand.Parameters("@ToEvent_ID").Value = Trim(cbNewEvent.SelectedValue)
        myCommand.Parameters("@Type").Value = GV_SELECTED_TYPE
        myCommand.Parameters("@TypeID").Value = GV_SELECTED_TYPE_ID
        myCommand.Parameters("@Pt_id1").Value = GV_CUR_PATID
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.ExecuteNonQuery()

        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing
        Close()
    End Sub

    Private Sub ChangeEventID()
        Dim response As Integer

        response = MsgBox("Are you sure want to move this record?", MsgBoxStyle.YesNo, "Move")
        If response = 7 Then
            Exit Sub
        End If

        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("spMoveEvent", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()

        myCommand.Parameters.Add("@FromEvent_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@ToEvent_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Type", SqlDbType.VarChar)
        myCommand.Parameters.Add("@TypeID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Pt_id1", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)

        myCommand.Parameters("@FromEvent_ID").Value = GV_SELECTED_EVENTID
        myCommand.Parameters("@ToEvent_ID").Value = Trim(cbNewEvent.SelectedValue)
        myCommand.Parameters("@Type").Value = GV_SELECTED_TYPE
        myCommand.Parameters("@TypeID").Value = GV_SELECTED_TYPE_ID
        myCommand.Parameters("@Pt_id1").Value = GV_CUR_PATID
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.ExecuteNonQuery()

        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing
        Close()
    End Sub
    Private Sub FrmMove_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtFromEventID.Text = GV_SELECTED_EVENTID & ", BTB ( " & LoadEventInfo(GV_SELECTED_EVENTID) & " )"
        LoadEventUnApprove()
    End Sub
    Function LoadEventInfo(ByVal GV_SELECTED_EVENTID)
        Dim vStr As String

        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        vStr = "select * from Event where Pt_id1 ='" & GV_CUR_PATID & "'  and Event_ID ='" & GV_SELECTED_EVENTID & "'"
        Dim sqlCmd = New SqlCommand(vStr, SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader

            Do While objReader.Read
                LoadEventInfo = IIf(IsDBNull(objReader("date_tb")), "01/01/1900", objReader("date_tb"))
            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Function
    Private Sub LoadEventUnApprove()

        Dim vStr As String

        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        vStr = "SELECT 'EVENT: '+ EVENT_ID + ', BTB ( ' + CONVERT(VARCHAR,date_tb,103) + ' )' AS EVENT_BTB, Pt_id1, EVENT_ID from Event where Pt_id1 ='" & GV_CUR_PATID & "' and Status ='A' and Event_ID <>'" & GV_SELECTED_EVENTID & "'"
        Dim sqlCmd = New SqlCommand(vStr, SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("Event_id", GetType(String)))
            list.Columns.Add(New DataColumn("EVENT_BTB", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("Event_id")), "", objReader("Event_id"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("EVENT_BTB")), "", objReader("EVENT_BTB"))
                i = i + 1
            Loop
            Me.cbNewEvent.DataSource = list
            Me.cbNewEvent.DisplayMember = "EVENT_BTB"
            Me.cbNewEvent.ValueMember = "Event_id"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Close()
    End Sub
End Class